//
//  PlaylistDBViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2017/8/2.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit

class PlaylistDBViewController: UIViewController {

	let member_uuid = "666"
	
    override func viewDidLoad() {
        super.viewDidLoad()
    }
	
	override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
		testPlaylistItem()
	}
	
	// MARK: - PlaylistItem
	
	func testPlaylistItem() {
//		addPlaylistItem()
//		addPlaylistItem()
//		getPlaylistItem()
//		updateDownloadCahce()
//		deletePlaylistItem()
//		getPlaylistItemCount()
//		getPlaylistItemList()
	}
	
	func getPlaylistItemList() {
		PlaylistItemDBManager.getPlaylistItemList(.mylike, member_uuid) { (list, msg) in
			list?.forEach({ (item) in
				printPlaylistItem(item)
			})
			DebugLog(msg)
		}
	}
	
	func getPlaylistItemCount() {
		PlaylistItemDBManager.getPlaylistItemCount(.mylike, "1", member_uuid) { (count, msg) in
			DebugLog(count)
			DebugLog(msg)
		}
	}
	
	func deletePlaylistItem() {
		PlaylistItemDBManager.deletePlaylistItem("666") { (success, msg) in
			printExecUpdateResult(success, msg)
		}
	}
	func updateDownloadCahce() {
		PlaylistItemDBManager.updateIsDownload("1", false) { (success, msg) in
			self.printExecUpdateResult(success, msg)
		}
		PlaylistItemDBManager.updateIsCache("1", false) { (success, msg) in
			self.printExecUpdateResult(success, msg)
		}
	}
	
	func getPlaylistItem() {
		let playlistItem = PlaylistItemModel()
		playlistItem.audio = generateAudio()
		playlistItem.member_uuid = member_uuid
		PlaylistItemDBManager.getPlaylistItem(.mylike, playlistItem) { (playlistItem, msg) in
			printPlaylistItem(playlistItem)
		}
	}
	
	func printPlaylistItem(_ playlistItem: PlaylistItemModel?) {
//        if let model = playlistItem {
//            DebugLog("id=\(model.id)")
//            DebugLog("member_uuid=\(model.member_uuid)")
//            DebugLog("order_num=\(model.order_num)")
//            DebugLog("playlist_id=\(model.playlist_id)")
//            DebugLog("playlist_type=\(model.playlist_type)")
//            DebugLog("other_data=\(model.other_data)")
//            DebugLog("is_cache=\(model.is_cache)")
//            DebugLog("is_download=\(model.is_download)")
//            DebugLog("file_path=\(model.file_path)")
//            DebugLog("audio_id=\(model.audio?.id)")
//            DebugLog("audio_name=\(model.audio?.name)")
//            DebugLog("audio_pic_url=\(model.audio?.pic_url)")
//            DebugLog("audio_track_name=\(model.audio?.track_name)")
//            DebugLog("audio_version=\(model.audio?.version)")
//        } else {
//            ErrorLog("playlistItem 为空")
//        }
	}
	
	func addPlaylistItem() {
		let playlistItem = generatePlaylistItem()
		
		PlaylistItemDBManager.addPlaylistItem(.mylike, playlistItem) { (success, msg) in
			self.printExecUpdateResult(success, msg)
		}
	}
	
	func generatePlaylistItem() -> PlaylistItemModel {
		let playlistItem = PlaylistItemModel()
		playlistItem.audio = generateAudio()
		playlistItem.file_path = "file_path"
		playlistItem.id = "1"
		playlistItem.is_cache = false
		playlistItem.is_download = false
		playlistItem.member_uuid = member_uuid
		playlistItem.order_num = 1
		playlistItem.other_data = "other_data"
		playlistItem.playlist_id = "1"
		playlistItem.playlist_type = .mylike
		return playlistItem
	}
	
	func generateAudio() -> AudioModel {
		let audio = AudioModel()
		audio.id = "1"
		audio.name = "My Dad"
		audio.pic_url = "pic_url"
		audio.track_name = "英文"
		audio.version = "1"
		return audio
	}
	
	func printExecUpdateResult(_ success: Bool, _ msg: String?) {
		DebugLog(success)
		DebugLog(msg)
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// MARK: - Playlist
	
	func testPlaylist() {
		addPlaylist()
		getPlaylist()
		updatePlaylistVersion()
		getPlaylist()
		getPlaylistCount()
		deletePlaylist()
		getPlaylistCount()
	}
	
	func getPlaylistCount() {
		PlaylistDBManager.getCount(member_uuid) { (count) in
			DebugLog(count)
		}
	}
	
	func deletePlaylist() {
		let model = generatePlaylistModel()
		PlaylistDBManager.deletePlaylist(model) { (success, msg) in
			self.printExecUpdateResult(success, msg)
		}
	}
	
	func updatePlaylistVersion() {
		let playlistModel = generatePlaylistModel()
		playlistModel.version = "18"
		PlaylistDBManager.updatePlaylistVersion(playlistModel) { (success, msg) in
			self.printExecUpdateResult(success, msg)
		}
	}
	
	func addPlaylist() {
		
		let playlistModel = generatePlaylistModel()
		
		PlaylistDBManager.addPlaylist(playlistModel) { (success, msg) in
			self.printExecUpdateResult(success, msg)
		}
	}
	
	func generatePlaylistModel() -> PlaylistModel {
		let playlistModel = PlaylistModel()
		playlistModel.id = "1"
		playlistModel.name = "1"
		playlistModel.member_uuid = member_uuid
		playlistModel.type = .mylike
		playlistModel.pic_url = ""
		playlistModel.version = "1"
		playlistModel.other_data = ""
		
		return playlistModel
	}
	
	func getPlaylist() {
		PlaylistDBManager.getPlaylist(member_uuid, .mylike) { [weak self] (playlistModel, msg) in
			self?.printPlaylist(playlistModel)
			DebugLog(msg)
		}
	}
	
	func printPlaylist(_ playlistModel: PlaylistModel?) {
//        if let playlistModel = playlistModel {
//            DebugLog("code=\(playlistModel.code)")
//            DebugLog("id=\(playlistModel.id)")
//            DebugLog("member_uuid=\(playlistModel.member_uuid)")
//            DebugLog("name=\(playlistModel.name)")
//            DebugLog("pic_url=\(playlistModel.pic_url)")
//            DebugLog("type=\(playlistModel.type)")
//            DebugLog("other_data=\(playlistModel.other_data)")
//            DebugLog("version=\(playlistModel.version)")
//        } else {
//            ExceptionLog("playlistModel == nil")
//        }
	}
	
	
}
